package com.xiaofu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaofu.interceptor.LoginInterceptor;
import com.xiaofu.mapper.AddressMapper;
import com.xiaofu.model.AddressDO;
import com.xiaofu.model.LoginUser;
import com.xiaofu.request.AddressAddRequest;
import com.xiaofu.service.AddressService;
import com.xiaofu.vo.AddressVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public AddressVO detail(Long id) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        Long userId = loginUser.getId();
        AddressDO addressDO = addressMapper.selectOne(new LambdaQueryWrapper<AddressDO>().eq(AddressDO::getId,id).eq(AddressDO::getUserId,userId));
        AddressVO addressVO = new AddressVO();
        BeanUtils.copyProperties(addressDO,addressVO);
        return addressVO;
    }

    /**
     * 新增收货地址
     * @param request
     * @return
     */
    @Override
    public int add(AddressAddRequest request) {

        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        AddressDO addressDO = new AddressDO();
        addressDO.setCreateTime(new Date());
        addressDO.setUserId(loginUser.getId());
        BeanUtils.copyProperties(request,addressDO);

        // 是否有默认收货地址
        if (addressDO.getDefaultStatus() == 1) {
            // 查找数据库是否有默认地址
            AddressDO defaultAddress = addressMapper.selectOne(new LambdaQueryWrapper<AddressDO>().eq(AddressDO::getUserId, loginUser.getId()));
            if (defaultAddress != null) {
                // 修改为非默认收货地址
                defaultAddress.setDefaultStatus(0);
                addressMapper.update(defaultAddress,new LambdaQueryWrapper<AddressDO>().eq(AddressDO::getId, defaultAddress.getId()));
            }
        }

        int rows = addressMapper.insert(addressDO);
        log.info("姓曾收货地址: rows={},data={}", rows,addressDO);
        return rows;
    }

    /**
     * 根据id删除地址
     * @param addressId
     * @return
     */
    @Override
    public int del(int addressId) {
        return addressMapper.delete(
                new LambdaQueryWrapper<AddressDO>()
                        .eq(AddressDO::getUserId, LoginInterceptor.threadLocal.get().getId())
                        .eq(AddressDO::getId, addressId));
    }

    /**
     * 查询用户的全部收货地址
     * @return
     */
    @Override
    public List<AddressVO> listUserAllAddresses() {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        List<AddressDO> addressDOS = addressMapper.selectList(new LambdaQueryWrapper<AddressDO>().eq(AddressDO::getUserId, loginUser.getId()));
        return addressDOS.stream().map(item -> {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(item,addressVO);
            return addressVO;
        }).collect(Collectors.toList());
    }

}
